package 牛客网_N.动态规划.最长公共子序列_13;

import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int n = Integer.parseInt(in.nextLine());
		int m = Integer.parseInt(in.nextLine());
		String A = in.nextLine();
		String B = in.nextLine();		
		in.close();
		
		System.out.println(findLCS(A, n, B, m));
	}
    public static int findLCS(String A, int n, String B, int m) {
		char[] a = new char[n];
		char[] b = new char[m];
		int[][] dp = new int[n+1][m+1];
		a = A.toCharArray();
		b = B.toCharArray();
		for(int i = 0; i < n; i++){
			dp[i][0] = 0;
		}
		for(int i = 0; i < m; i++){
			dp[0][i] = 0;
		}
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j++){
				if(a[i-1] == b[j-1]){
					dp[i][j] = dp[i-1][j-1]+1;
				}else{
					dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
				}
			}
		}
		return dp[n][m];
    }
		
}
